package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo09 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        //获取连接
        try (
                Connection conn = DBUtils.getConn();
                ){
            String sql = "select password from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){ //如果查得到代表用户名存在
                String pw = rs.getString(1);//得到查询出来的密码
                //拿用户输入的和数据库中正确的密码 比较
                if (pw.equals(password)){
                    System.out.println("登录成功");
                }else {
                    System.out.println("密码错误");
                }
                return;
            }
            System.out.println("用户不存在");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
